package cn.com.haopy.yue.ai.wx.web;


import cn.com.haopy.yue.ai.core.utils.ResponseUtil;
import cn.com.haopy.yue.ai.core.validator.Order;
import cn.com.haopy.yue.ai.core.validator.Sort;
import cn.com.haopy.yue.ai.db.entity.arisk.AIRiskPapers;
import cn.com.haopy.yue.ai.db.entity.arisk.AIRiskPapersDrugs;
import cn.com.haopy.yue.ai.db.entity.arisk.AIRiskPapersIllness;
import cn.com.haopy.yue.ai.db.service.hos.AIRiskPapersDrugsService;
import cn.com.haopy.yue.ai.db.service.hos.AIRiskPapersIllnessService;
import cn.com.haopy.yue.ai.db.service.hos.AIRiskPapersOptionsService;
import cn.com.haopy.yue.ai.db.service.hos.AIRiskPapersService;
import cn.com.haopy.yue.ai.wx.annotation.LoginUser;
import cn.com.haopy.yue.ai.wx.dto.WxReqPaperDto;
import cn.com.haopy.yue.ai.wx.service.WxAIRiskPapersService;
import cn.com.haopy.yue.ai.wx.service.WxPapersDrugsService;
import cn.com.haopy.yue.ai.wx.service.WxPapersPresIllnessService;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import java.util.*;

/**
 * 对象存储服务
 */
@RestController
@RequestMapping("/haopyYueAi/papers")
@Api(tags = "问卷相关")
@Slf4j
public class WxRiskPaperController {

    @Autowired
    private WxAIRiskPapersService wxRiskPapersService;

    @Autowired
    AIRiskPapersService riskPapersService;

    @Autowired
    AIRiskPapersIllnessService riskPapersIllnessService;

    @Autowired
    WxPapersDrugsService wxPapersDrugsService;

    @Autowired
    AIRiskPapersOptionsService riskPapersOptionsService;

    @Autowired
    WxPapersPresIllnessService wxPapersPresIllnessService;

    @Autowired
    private AIRiskPapersDrugsService riskPapersDrugsService;


    @PostMapping("/createPaper")
    @ApiOperation("生成问卷")
    public Object createPaper(@LoginUser Integer userId, @RequestBody @Validated WxReqPaperDto wxReqPaperDto) throws Exception {
        if (Objects.isNull(userId)) {
            return ResponseUtil.unlogin();
        }
        //return wxRiskPapersService.createPaper(userId, wxReqPaperDto);
        return wxPapersPresIllnessService.createPaperByPreHaopy(userId, wxReqPaperDto);
    }

    @PostMapping("/createPaperByPdfJson")
    @ApiOperation("生成问卷-pdf数据")
    public Object createPaperByPdfJson(@LoginUser @ApiIgnore Integer userId, @RequestBody JSONObject pdfJsonData) throws Exception {
        if (Objects.isNull(userId)) {
            return ResponseUtil.unlogin();
        }
        //return wxRiskPapersService.createPaperByPdfJson(userId, pdfJsonData);
        return wxPapersPresIllnessService.createPaperByPreIllnessPdfJson(userId, pdfJsonData);
    }

    @GetMapping("/paperInfo")
    @ApiOperation("问卷信息")
    public Object paperInfo(@LoginUser Integer userId, @RequestParam Integer paperId) {
        if (Objects.isNull(userId)) {
            return ResponseUtil.unlogin();
        }
        return ResponseUtil.ok(riskPapersService.getById(paperId));
    }

    @GetMapping("/paperMaxSort")
    @ApiOperation("问卷最大索引")
    public Object paperMaxSort(@LoginUser Integer userId, @RequestParam Integer paperId) {
        if (Objects.isNull(userId)) {
            return ResponseUtil.unlogin();
        }
        return ResponseUtil.ok(riskPapersOptionsService.paperMaxSortIndex(paperId));
    }

    @ApiOperation("问卷-记录")
    @GetMapping("/paperRecord")
    public Object paperRecord(@LoginUser Integer userId,
                              @RequestParam(defaultValue = "1") Integer pageNo,
                              @RequestParam(defaultValue = "10") Integer pageSize,
                              @Sort @RequestParam(defaultValue = "create_time") String sort,
                              @Order @RequestParam(defaultValue = "desc") String order) {
        if (Objects.isNull(userId)) {
            return ResponseUtil.unlogin();
        }
        Page<AIRiskPapers> riskTopicList = riskPapersService.querySelective(userId, pageNo, pageSize, sort, order);
        return ResponseUtil.okPage(riskTopicList);
    }

    @GetMapping("/submitPaper")
    @ApiOperation("问卷-提交评估")
    public Object submitPaper(@LoginUser Integer userId, @RequestParam Integer paperId) {
        if (Objects.isNull(userId)) {
            return ResponseUtil.unlogin();
        }
        return wxRiskPapersService.submitPaper(paperId);
    }

    @GetMapping("/paperIllnessRisk")
    @ApiOperation("问卷-疾病结果")
    public Object paperIllnessRisk(@LoginUser Integer userId, @RequestParam Integer paperId) {
        if (Objects.isNull(userId)) {
            return ResponseUtil.unlogin();
        }
        AIRiskPapers papers = riskPapersService.getById(paperId);
        if (Objects.nonNull(papers) && StringUtils.hasText(papers.getPdfUrl())) {
            Map<String, Object> data = new HashMap<>();
            data.put("paper", papers);
            data.put("illnessList", riskPapersIllnessService.getByHighRiskPapersId(paperId));
            data.put("paperDrugs", wxPapersDrugsService.getDrugsByPaperId(paperId));
            return ResponseUtil.ok(data);
        }
        return ResponseUtil.fail("结果未生成！");

    }


    @GetMapping("/paperDetail")
    @ApiOperation("问卷-详情")
    public Object paperDetail(@LoginUser Integer userId, @RequestParam Integer paperId) {
        if (Objects.isNull(userId)) {
            return ResponseUtil.unlogin();
        }
        AIRiskPapers papers = riskPapersService.getById(paperId);
        if (Objects.nonNull(papers)) {
            Map<String, Object> data = new HashMap<>();
            data.put("paper", papers);
            List<AIRiskPapersIllness> paperIllnessList = riskPapersIllnessService.getByHighRiskPapersId(paperId);

            List<AIRiskPapersDrugs> papersDrugsList = riskPapersDrugsService.getByPaperId(paperId);
            List<Map<String, Object>> listMap = new ArrayList<>();
            for (AIRiskPapersIllness papersIllnessItem : paperIllnessList) {
                Map<String, Object> map = new HashMap<>();
                map.put("illness", papersIllnessItem);
                //药品
                for (int i = 0; i < papersDrugsList.size(); i++) {
                    AIRiskPapersDrugs papersDrugsItem = papersDrugsList.get(i);
                    String[] drugs = papersDrugsItem.getDrugNames();
                    if (papersDrugsItem.getIllnessId().equals(papersIllnessItem.getIllnessId())) {
                        map.put("drugs", drugs);
                        break;
                    }
                }

                listMap.add(map);

            }
            data.put("illnessList", listMap);
            return ResponseUtil.ok(data);
        }
        return ResponseUtil.fail("问卷不存在！");

    }
}